package com.cr.backbone.ajax.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.mail.internet.MimeUtility;

import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.model.SysFile;
import com.cr.backbone.service.business.sysfile.SysFileService;

/**
 * 
 * 文件上传Action
 * 
 * @author huxh
 * @date 2012-6-11
 */
public class AjaxDownloadFileAction extends AppBaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1179031234511250745L;

	private static final String NO_FILE = "nofile";
	private static final String DOWNLOAD = "download";

	private SysFileService sysFileService;
	private Long sysFileId;// 传入sysFile 文件ID
	private InputStream inputStream;// 返回文件流
	private Long size;// 文件大小
	private String name;// 文件名称
	private String contentType;// 文件类型

	public String download() {
		if (sysFileId == null || sysFileId < 0)
			return NO_FILE;

		try {
			SysFile sysfile = sysFileService.get(sysFileId);
			name = MimeUtility.encodeWord(sysfile.getFileTitle());
			contentType = sysfile.getFileType();
			File file = new File(sysfile.getFileRealPath());
			inputStream = new FileInputStream(file);
			size = file.length(); 

		} catch (Exception e) {
			//e.printStackTrace();
			return NO_FILE;
		}
		return DOWNLOAD;
	}

	public SysFileService getSysFileService() {
		return sysFileService;
	}

	public void setSysFileService(SysFileService sysFileService) {
		this.sysFileService = sysFileService;
	}

	public Long getSysFileId() {
		return sysFileId;
	}

	public void setSysFileId(Long sysFileId) {
		this.sysFileId = sysFileId;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Long getSize() {
		return size;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

}
